package io.finer.erp.jeecg.bas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import io.finer.erp.jeecg.bas.dto.SupplierCategoryCountDTO;
import io.finer.erp.jeecg.bas.entity.BasSupplier;
import io.finer.erp.jeecg.bas.entity.BasSupplierCategory;
import io.finer.erp.jeecg.bas.mapper.BasSupplierCategoryMapper;
import io.finer.erp.jeecg.bas.mapper.BasSupplierMapper;
import io.finer.erp.jeecg.bas.service.IBasSupplierCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 供应商分类
 * @Author: jeecg-boot
 * @Date:   2021-05-27
 * @Version: V1.0
 */
@Service
public class BasSupplierCategoryServiceImpl extends ServiceImpl<BasSupplierCategoryMapper, BasSupplierCategory> implements IBasSupplierCategoryService {

    @Autowired
    private BasSupplierMapper basSupplierMapper;

    @Override
    public List<SupplierCategoryCountDTO> getCategoryList() {
        List<SupplierCategoryCountDTO> result = Lists.newArrayList();
        List<BasSupplierCategory> allList = this.baseMapper.selectList(new QueryWrapper<BasSupplierCategory>());
        for(BasSupplierCategory bsc:allList){
            SupplierCategoryCountDTO scc = new SupplierCategoryCountDTO();
            scc.setCategoryName(bsc.getName());
            QueryWrapper<BasSupplier> sumWrapper = new QueryWrapper<BasSupplier>();
            sumWrapper.lambda().like(BasSupplier::getCategoryId,bsc.getId());
            Integer sum = this.basSupplierMapper.selectCount(sumWrapper);
            scc.setSum(sum);
            result.add(scc);
        }
        return result;
    }
}
